package org.gruposp2p.dnie.client.ui.dialogs;

import java.util.List;
import org.gruposp2p.dnie.client.dto.DocumentToSignDTO;
import org.gruposp2p.dnie.client.dto.StatisticDTO;
import org.gruposp2p.dnie.client.dto.StatisticOptionDTO;
import org.gruposp2p.dnie.client.event.EventBusManager;
import org.gruposp2p.dnie.client.event.StatisticChangeEvent;
import org.gruposp2p.dnie.client.event.StatisticChangeHandler;
import org.gruposp2p.dnie.client.json.JSONToDTO;
import org.gruposp2p.dnie.client.server.ServerFacade;
import org.gruposp2p.dnie.client.server.StatusData;
import org.gruposp2p.dnie.client.ui.panel.MainPanel;
import org.gruposp2p.dnie.client.ui.widget.GlassedDialog;
import org.gruposp2p.dnie.client.util.URLConstants;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

public class DocumentToSignDetailsStatisticsPanel extends Composite 
	implements StatisticChangeHandler{

    public DocumentToSignDetailsStatisticsPanel() { }
    
    DocumentToSignDTO documentToSign;
    FlexTable layout;
    CellFormatter cellFormatter;

    public DocumentToSignDetailsStatisticsPanel(DocumentToSignDTO document) {
        EventBusManager.getInstance().registerToStatisticChange(this);
        this.documentToSign = document;
        layout = new FlexTable();
        cellFormatter = layout.getCellFormatter();
        initWidget(layout);
        ServerFacade.doGet(
                URLConstants.getStatisticGetUrl(documentToSign.id),
                new ServerRequestCallback());
    }
    
    private void repaintPanel (StatisticDTO statisticDTO) {
        layout.clear();
        layout.setCellSpacing(6);
        int widgetRow = 0;
        HTML numberOfSignatures = new HTML(
        		MainPanel.constants.NumberOfSignatures() + ": " + statisticDTO.total);
        numberOfSignatures.setStyleName("PanelSectionText");
        layout.setWidget(widgetRow++, 0, numberOfSignatures);
        List<StatisticOptionDTO> options = statisticDTO.options;
        if (options != null) {
            HTML optionsText = new HTML(MainPanel.constants.OptionsSelected());
            optionsText.setStyleName("PanelSectionText");        
            layout.setWidget(widgetRow++, 0, optionsText);
            for (StatisticOptionDTO option : options) {
            	HTML optionText = new HTML(option.name + ":" + option.total);
            	optionText.setStyleName("StatisticsPanelOption"); 
            	layout.setWidget(widgetRow++, 0, optionText);    	
            }
        }
    }
    
    private class ServerRequestCallback implements RequestCallback {

        @Override
        public void onError(Request request, Throwable exception) {
                StatusData status = new StatusData();
                status.description = exception.getMessage();
                GlassedDialog.getInstance().showErrorGettingResource(status);
        }

        @Override
        public void onResponseReceived(Request request, Response response) {
            if (response.getStatusCode() == Response.SC_OK) {
                JSONValue jsonValue = JSONParser.parse(response.getText());
                StatisticDTO statisticDTO = JSONToDTO.getStatisticDTO(jsonValue);
                StatisticChangeEvent statisticChangeEvent =
                        new StatisticChangeEvent(statisticDTO);
                EventBusManager.getInstance().fireEvent(statisticChangeEvent);
            } else {
                StatusData statusData = new StatusData ();
                statusData.code = String.valueOf(response.getStatusCode());
                statusData.description = response.getText();
                GlassedDialog.getInstance().showErrorGettingResource(statusData);
            }
        }

    }


    @Override
    public void processStatistic(StatisticChangeEvent event) {
            repaintPanel(event.statistic);
    }

}